/*
 * Copyright 2023-2085 xujin <ceo@itgrail.com>
 *
 * Licensed under the Grail License, Version 1.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * https://itgrail.com/docs?id=3&catalogId=87
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.itgrail.pilot.infrastructure.tunnel.db.dao;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.itgrail.pilot.domain.user.contant.OkrConstants;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.DepartmentUserDO;
import com.itgrail.pilot.infrastructure.tunnel.db.dataobject.UserDO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author xujin
 * @create 2023/4/12 21:04
 */
@Mapper
public interface DepartmentUserMapper extends BaseMapper<DepartmentUserDO> {
    default Long selectUserCount(List<Integer> deptIdList) {
        LambdaQueryWrapper<DepartmentUserDO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DepartmentUserDO::getIsDeleted, OkrConstants.NOT_DELETED);
        queryWrapper.in(CollUtil.isNotEmpty(deptIdList), DepartmentUserDO::getDepartmentId, deptIdList);
        return selectCount(queryWrapper);
    }

    List<DepartmentUserDO> selectUserList(List<Integer> deptIdList);

    default List<Integer> selectUserIdList(List<Integer> deptIdList) {
        List<DepartmentUserDO> userList = selectUserList(deptIdList);
        return userList.stream().map(DepartmentUserDO::getUser).filter(data -> data.getUserId() != null).map(UserDO::getUserId).distinct().collect(Collectors.toList());
    }
    @Select("SELECT * FROM t_department_user WHERE user_id=#{userId} AND is_deleted=0")
    List<DepartmentUserDO> queryByUserId(Integer userId );
}
